package com.tyj.movie.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tyj.movie.entity.Movie;
import com.tyj.movie.entity.MovieTheatre;
import com.tyj.movie.entity.MovieTheatreServer;
import com.tyj.movie.entity.MovieTheatreServerRelation;
import com.tyj.movie.mapper.MovieTheatreMapper;
import com.tyj.movie.mapper.MovieTheatreServerMapper;
import com.tyj.movie.mapper.MovieTheatreServerRelationMapper;
import com.tyj.movie.service.MovieTheatreServerRelationService;
import com.tyj.movie.service.MovieTheatreService;
import com.tyj.movie.to.MovieTheatreTo;
import com.tyj.movie.vo.MovieTheatreServerVo;
import com.tyj.movie.vo.MovieTheatreVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tyj
 * @since 2021-06-28
 */
@Service
public class MovieTheatreServiceImpl extends ServiceImpl<MovieTheatreMapper, MovieTheatre> implements MovieTheatreService {

    @Autowired
    MovieTheatreMapper movieTheatreMapper;

    @Autowired
    MovieTheatreServerMapper movieTheatreServiceMapper;

    @Autowired
    MovieTheatreServerRelationMapper movieTheatreServerRelationMapper;

    @Autowired
    MovieTheatreServerRelationService movieTheatreServerRelationService;



    @Override
    public void addMovieTheatre(MovieTheatreTo movieTheatreTo) {
        //添加影院
        MovieTheatre movieTheatre =new MovieTheatre();
        BeanUtils.copyProperties(movieTheatreTo,movieTheatre);
        movieTheatre.setCreateDate(new Date());
        movieTheatreMapper.insert(movieTheatre);

        //添加影院服务
        String movieTheatreService = movieTheatreTo.getMovieTheatreService();
        String[] service = movieTheatreService.split(",");
        List<MovieTheatreServerRelation> list =new ArrayList<>();
        for (String s : service) {
            String[] split = s.split(":");
            MovieTheatreServerRelation movieTheatreServerRelation=new MovieTheatreServerRelation();
//            id  movie_theatre_id  movie_theatre_server_id  server
            movieTheatreServerRelation.setMovieTheatreId(movieTheatre.getId());
            movieTheatreServerRelation.setMovieTheatreServerId(Integer.valueOf(split[0]));
            movieTheatreServerRelation.setServer(split[1]);
            list.add(movieTheatreServerRelation);
        }
        movieTheatreServerRelationService.saveBatch(list);

    }

    @Override
    public IPage<MovieTheatreVo> queryMovieTheatre(Long pageNum) {
        IPage<MovieTheatreVo> movieTheatreVoIPage = movieTheatreMapper.queryMovieTheatre(new Page<>(pageNum,5));

        //循环查出服务
        List<MovieTheatreVo> records = movieTheatreVoIPage.getRecords();
        for (MovieTheatreVo record : records) {
          List<MovieTheatreServerVo> movieTheatreServerVoList  = movieTheatreServerRelationMapper.queryMovieTheatreServerById(record.getId());
          record.setMovieTheatreServerVos(movieTheatreServerVoList);
        }

        return movieTheatreVoIPage;
    }

    @Override
    public void enable(Long id) {
        movieTheatreMapper.recoveryById(id);
    }

    @Override
    public void disable(Long id) {
        movieTheatreMapper.deleteById(id);
    }
}
